<?php
class Mcase_product extends CI_Model{
	protected $_table="tpt_case_product";
	protected $_table_2="tpt_catalogue_product";
	protected $_table_3="tpt_product";
	public function __construct(){
		parent::__construct();	
		$this->load->database();
	} 	

	public function listCase(){
        $sSortDir = $this->input->get_post('sSortDir_0', true);
        
        /* Array of database columns which should be read and sent back to DataTables. Use a space where
         * you want to insert a non-database field (for example a counter or static image)
         */
        $sTable = $this->_table;
        $sTable_2 = $this->_table_2;
        $sTable_3 = $this->_table_3;

        $aColumns = array("{$sTable}.case_id","{$sTable_3}.pro_title","{$sTable_2}.cata_name","{$sTable}.case_title","{$sTable}.date_add");
        
        $aColumns2 = array('cata_name','pro_title','case_id','case_title','date_add');
        
        // DB table to use
        $this->db->join($sTable_2,$sTable.'.cata_id = '.$sTable_2.'.cata_id','left');
        $this->db->join($sTable_3,$sTable.'.pro_id = '.$sTable_3.'.pro_id','left');

        $this->db->order_by("{$sTable}.case_id","DESC");

        $iDisplayStart = $this->input->get_post('iDisplayStart', true);
        $iDisplayLength = $this->input->get_post('iDisplayLength', true);
        $iSortCol_0 = $this->input->get_post('iSortCol_0', true);
        $iSortingCols = $this->input->get_post('iSortingCols', true);
        $sSearch = $this->input->get_post('sSearch', true);
        $sEcho = $this->input->get_post('sEcho', true);
        $myFilter = $this->input->get_post('myFilter', true);
    
        // Paging
        if(isset($iDisplayStart) && $iDisplayLength != '-1')
        {
            $this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
        }
        
        // Ordering
        if(isset($iSortCol_0))
        {
            for($i=0; $i<intval($iSortingCols); $i++)
            {
                $iSortCol = $this->input->get_post('iSortCol_'.$i, true);
                $bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
                $sSortDir = $this->input->get_post('sSortDir_'.$i, true);
    
                if($bSortable == 'true')
                {
                    $this->db->order_by($aColumns[intval($this->db->escape_str($iSortCol)) + 1], $this->db->escape_str($sSortDir));
                }
            }
        }
        
        /* 
         * Filtering
         * NOTE this does not match the built-in DataTables filtering which does it
         * word by word on any field. It's possible to do here, but concerned about efficiency
         * on very large tables, and MySQL's regex functionality is very limited
         */
        if(isset($sSearch) && !empty($sSearch))
        {
            for($i=0; $i<count($aColumns); $i++)
            {
                $bSearchable = $this->input->get_post('bSearchable_'.$i, true);
                
                // Individual column filtering
                if(isset($bSearchable) && $bSearchable == 'true')
                {
                    $this->db->or_like($aColumns[$i], $this->db->escape_like_str($sSearch));
                }
            }
        }

        // my filter
        if(isset($myFilter) && !empty($myFilter)){
            $myFilter = json_decode($myFilter);
            foreach($myFilter as $filter => $keyword){
                if($keyword != 'all'){
                    $this->db->where($filter, $keyword);
                }
            }
        }
        
        // Select Data
        $this->db->select('SQL_CALC_FOUND_ROWS '.str_replace(' , ', ' ', implode(', ', $aColumns)), false);
        $rResult = $this->db->get($sTable);
    
        // Data set length after filtering
        $this->db->select('FOUND_ROWS() AS found_rows');
        $iFilteredTotal = $this->db->get()->row()->found_rows;
    
        // Total data set length
        $iTotal = $this->db->count_all($sTable);
    
        // Output
        $output = array(
            'sEcho' => intval($sEcho),
            'iTotalRecords' => $iTotal,
            'iTotalDisplayRecords' => $iFilteredTotal,
            'aaData' => array()
        );
        
        foreach($rResult->result_array() as $aRow)
        {
            $row = array();
            
            foreach($aColumns2 as $col)
            {
                if(!in_array($col, array('case_id'))){
                    $value = '';
                    $value = $aRow[$col];
                    $row[] = $value;
                }
            }
            $row[] = '  <a href="' . base_url() . 'admincp/case_product/edit/id/' . $aRow['case_id'] . '" class="btn btn-default tip" title="Edit"><i class="icon16 i-pencil"></i></a>
                        <a href="javascript:void(0)" onclick="confirm_delete('.$aRow['case_id'].',\''.$this->router->fetch_class().'\');" class="btn btn-default tip" title="Delete"><i class="icon16 i-remove"></a>';
            $output['aaData'][] = $row;
            $output['my_id'][] = $aRow['case_id'];
        }
        return $output;
    }
	
	public function insert($data){
		$this->db->insert($this->_table,$data);	
	}

    public function get_data($id){
        $this->db->where('case_id',$id);
        return $this->db->get($this->_table)->row_array();
    }

    public function update($data,$id){
        $this->db->where('case_id',$id);
        $this->db->update($this->_table,$data);
    }

    public function delete($id){
        $this->db->where('case_id',$id);
        $this->db->delete($this->_table,$data);
    }	
}